package com.atguigu.gulimail.member.interceptor;

import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    public static final ThreadLocal<MemberResponseVo> threadLocal=new ThreadLocal();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            //getUri是路径 geturl是http://...加路径
        String uri=request.getRequestURI();
        //路径匹配器
        boolean match = new AntPathMatcher().match("/member/**", uri);
        if (match){
            return true;
        }


        MemberResponseVo attribute = (MemberResponseVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute!=null){
            //说明登录了
            //那么把登录信息放到ThreadLocal
            threadLocal.set(attribute);
            return true;
        }else{
                //没登录
            response.sendRedirect("http://auth.gulimail.com/login.html");
            request.getSession().setAttribute("msg", "请先登录");
            return false;
        }

    }
}
